import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import Home from '../views/Home.vue'
import myfome from '../components/MyFome.vue'
import mymenu from '../components/MyMenu.vue'
import register from '../views/registered'
import jwtDecode from 'jwt-decode'


Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'login',
    component: Login
  },
  {
    path: '/Home',
    name: 'home',
    component: Home,
    children:[
      {
        path: '/money',
        name: 'money',
        component: () => import('../views/fund/money.vue')
      }
    ]
  },
  // 封装的表单组件
  {
    path: '/myfome',
    name: 'myfome',
    component: myfome
  },
  // 封装的侧边导航菜单
  {
    path: '/mymenu',
    name: 'mymenu',
    component: mymenu
  },
  {
    path: '/register',
    name: 'register',
    component: register
  }
  // {
  //   path: '/about',
  //   name: 'about',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  // }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

export default router
// 路由守卫
router.beforeEach((to,from,next)=>{
  if(to.path=='/' || to.path=='/register') return next()
  var token=localStorage.getItem('token')
  if(!token){
    next('/')
  }else{
    // 获取超时时间
    let {exp,name} = jwtDecode(token);
    let nowtime = new Date().getTime()
    nowtime = Math.floor(nowtime/1000);
    // 判断是否超时
    if(exp - nowtime >=0){
      next();
    }else{
      next('/')
    }
  }
  next()
})
